﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ObviousCode.Stately.Compiler.Parser
{
    public class TransformData
    {
        public TransformData()
            : this("ObviousCode", "GeneratedWorkflowClient")
        {

        }

        public TransformData(string namespc, string className)
        {
            Namespace = namespc;
            ClassName = className;

            States = new List<string>();
            Transitions = new List<Transition>();

            Code = new TransformCodeBlocks(this); 
        }

        public string StartingState { get; set; }

        public List<string> States { get; private set; }
        public List<Transition> Transitions { get; private set; }

        public string Namespace { get; set; }
        public string ClassName { get; set; }
        public string DataContextName { get; set; }

        public bool UseDataContext { get { return !(string.IsNullOrWhiteSpace(DataContextName)); } }
        public TransformCodeBlocks Code { get; private set; }

        public List<Transition> SimpleTransitions
        {
            get
            {
                List<Transition> transitions = new List<Transition>();

                States.ForEach(s => transitions.AddRange(GetSimpleTransitionsForState(s)));

                return transitions;
            }
        }
        public IEnumerable<Transition> GetSimpleTransitionsForState(string state)
        {
            return Transitions
                        .Where(t => t.SourceState.Data == state);
        }

        public List<Transition> WildcardTransitions
        {
            get
            {
                List<Transition> transitions = new List<Transition>();

                States.ForEach(s => transitions.AddRange(GetWildcardTransitionsForState(s)));

                return transitions;
            }
        }
        public IEnumerable<Transition> GetWildcardTransitionsForState(string state)
        {
            return Transitions
                        .Where(t => t.SourceNodeType == Transition.NodeType.WildCard &&
                                    t.DestinationState.Data == state);
        }

        public string StartTransitionName
        {
            get { return StartingState; }
        }

        public string DataContextInstanceName
        {
            get
            {
                if (!UseDataContext) return "";

                return "context";

                //return "{0}{1}"
                //    .F(
                //        DataContextName[0].ToString().ToLowerInvariant(),
                //        DataContextName.Substring(1)
                //    );
            }
        }        
    }
}
